package pcloud.client.remoteservice;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import pcloud.client.util.FileResourceInfo;

public class FTPServiceTest
{
	FileService fileService;

	@Before
	public void setUp() throws Exception
	{
		fileService = FTP4JService
				.createInstance("localhost", "ftpuser", "ftp");
	}

	@After
	public void tearDown() throws Exception
	{
		fileService.release();
	}

	@Test
	public void testCreateFolder()
	{
		try {
			String path = "/FTPServiceTest" + new Date().getTime()
					+ "/";
			if (fileService.createFolder(path)) {
				fileService.deleteFolder(path);
			}
			
		} catch (Exception e) {
			fail(e.getMessage());
		}
		
	}

	@Test
	public void testCreateFolderMixName()
	{
		try {
			String path = "/测试文件夹" + new Date().getTime() + "/";
			if (fileService.createFolder(path)) {
				fileService.deleteFolder(path);
			}
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}

	// @Test
	// public void testDelete() {
	// fail("Not yet implemented");
	// }

	@Test
	public void testUploadFileTxt()
	{
		FileResourceInfo file = new FileResourceInfo(new File(
				"D:/pcloud/WatchTest/j.txt"));
		file.setRootPath("D:/pcloud/WatchTest/");
		try {
			fileService.uploadFile(file);
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testUploadFileChineseName()
	{
		FileResourceInfo file = new FileResourceInfo(new File(
				"D:/pcloud/WatchTest/新建文本文档.txt"));
		file.setRootPath("D:/pcloud/WatchTest/");
		try {
			fileService.uploadFile(file);
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testUploadFileMixName()
	{
		FileResourceInfo file = new FileResourceInfo(new File(
				"D:/pcloud/WatchTest/j - 副本.txt"));
		file.setRootPath("D:/pcloud/WatchTest/");
		try {
			fileService.uploadFile(file);
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testUploadFileLargeBin()
	{
		FileResourceInfo file = new FileResourceInfo(new File(
				"D:/pcloud/WatchTest/大侦探福尔摩斯2.mp4"));
		file.setRootPath("D:/pcloud/WatchTest/");
		try {
			fileService.uploadFile(file);
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testDownloadFileTxt()
	{
		FileResourceInfo file = new FileResourceInfo();
		file.setRemote(true);
		file.setFullPath("/j.txt");
		file.setRootPath("/");
		try {
			fileService.downloadFile(file);
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testDownloadFileChineseName()
	{
		FileResourceInfo file = new FileResourceInfo();
		file.setRemote(true);
		file.setFullPath("/新建文本文档.txt");
		file.setRootPath("/");
		try {
			fileService.downloadFile(file);
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testDownloadFileMixName()
	{
		FileResourceInfo file = new FileResourceInfo();
		file.setRemote(true);
		file.setFullPath("/j - 副本.txt");
		file.setRootPath("/");
		try {
			fileService.downloadFile(file);
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testDownloadFileLargeBin()
	{
		FileResourceInfo file = new FileResourceInfo();
		file.setRemote(true);
		file.setFullPath("/大侦探福尔摩斯2.mp4");
		file.setRootPath("/");
		try {
			fileService.downloadFile(file);
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testList()
	{
		try {
			List<FileResourceInfo> files = fileService.list("/ListTest");
			assertTrue( files.size()==4);
			System.out.println("filename\t\t\tfilesize\t\t\tlastmodified");
			for (FileResourceInfo file : files) {
				System.out.println(file.getFilename()+"\t\t\t"+file.isFolder()+"\t\t\t"+file.getLength()+"\t\t"+file.getLastModified());
			}
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testGetResource()
	{
		FileResourceInfo file;
		try {
			file = fileService.getResource("/2.pptx");
			assertTrue( file.getFilename().equals("2.pptx"));
			assertTrue( file.getLength()==34242);
			assertTrue( file.isFolder()==false);
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testDeleteFile()
	{
		try {
			assertTrue( fileService.deleteFile("/j.txt") );
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testDeleteFileChineseName()
	{
		try {
			assertTrue( fileService.deleteFile("/新建文本文档.txt") );
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testDeleteFileMixName()
	{
		try {
			assertTrue( fileService.deleteFile("/j - 副本.txt") );
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void testDeleteFileLargebin()
	{
		try {
			assertTrue( fileService.deleteFile("/大侦探福尔摩斯2.mp4") );
		} catch (IOException e) {
			fail(e.getMessage());
		}
	}

}
